/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.sonar.model;

import java.util.TreeSet;
import java.util.Comparator;
import java.util.Collection;
import java.util.Collections;
import org.internna.iwebmvc.utils.CollectionUtils;
import org.internna.iwebjtracker.sonar.services.SonarManager.METRICS;

/**
 * Holds a number of metrics and values.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class SonarMetrics {

    private Collection<SonarMetric> metrics;

    /**
     * Obtain all stored metrics at once.
     *
     * @return a non null unmodifiable collection
     */
    public Collection<SonarMetric> getMetrics() {
        return CollectionUtils.isNotEmpty(metrics) ? Collections.unmodifiableCollection(metrics) : Collections.EMPTY_SET;
    }

    /**
     * Creates a new metric instance.
     *
     * @param metric any (null metrics generate 
     * @return
     */
    public static SonarMetric createMetric(METRICS metric) {
        return metric == null ? null : new SonarMetric(metric);
    }

    /**
     * Stores a new metric.
     *
     * @param metric any (null values are not stored)
     */
    public void addMetric(SonarMetric metric) {
        if (metric != null) {
            if (metrics == null) {
                metrics = new TreeSet<SonarMetric>(new Comparator<SonarMetric>() {
                    @Override public int compare(SonarMetric metric, SonarMetric other) {
                        return metric.getMetric().compareTo(other.getMetric());
                    }

                });
            }
            metrics.add(metric);
        }
    }

    /**
     * Obtains the metric stored value.
     *
     * @param metric any
     * @return any
     */
    public SonarMetric getMetric(METRICS metric) {
        if ((metric != null) && (CollectionUtils.isNotEmpty(metrics))) {
            SonarMetric checked = new SonarMetric(metric);
            for (SonarMetric loaded : metrics) {
                if (loaded.equals(checked)) {
                    return loaded;
                }
            }
        }
        return null;
    }

    /**
     * Simple Javabean to hold a metric and the value obtained.
     */
    public static final class SonarMetric {

        /**
         * Builds a new instance for the given metric.
         * @param metric
         */
        private SonarMetric(METRICS metric) {
            this.metric = metric;
        }

        private double value;
        private String formatting;
        private final METRICS metric;

        /**
         * Accessor method.
         *
         * @return any
         */
        public String getFormatting() {
            return formatting;
        }

        /**
         * Accessor method.
         *
         * @param formatting any
         */
        public void setFormatting(String formatting) {
            this.formatting = formatting;
        }

        /**
         * Accessor method.
         *
         * @return any
         */
        public METRICS getMetric() {
            return metric;
        }

        /**
         * Accessor method.
         *
         * @return any
         */
        public double getValue() {
            return value;
        }

        /**
         * Accessor method.
         *
         * @param value any
         */
        public void setValue(double value) {
            this.value = value;
        }

        /**
         * Compares just the METRIC field.
         *
         * @param obj any
         * @return true if the metric matches.
         */
        @Override public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            return this.metric == ((SonarMetric) obj).metric;
        }

        /**
         * Hascode considering the metric only.
         *
         * @return a number
         */
        @Override public int hashCode() {
            return 67 + this.metric.hashCode();
        }

    }

}
